package com.jbm.core.common;

import com.jbm.core.client.MainCallback;

import java.util.HashMap;

/**
 * Created by root on 17-2-24.
 */
public class IAccount
{
	protected Integer m_account_id;

	public Integer getM_seller_id() {
		return m_seller_id;
	}

	public void setM_seller_id(Integer m_seller_id) {
		this.m_seller_id = m_seller_id;
	}

	public Integer getM_member_id() {
		return m_member_id;
	}

	public void setM_member_id(Integer m_member_id) {
		this.m_member_id = m_member_id;
	}

	protected Integer m_seller_id;
	protected Integer m_member_id;

	protected final Object m_map_mutex = new Object();

	protected HashMap<String,ConnectAgent> m_apptypemap;
	protected HashMap<Integer,ConnectAgent> m_devicemap;

	public IAccount()
	{
		m_account_id = 0;

		m_apptypemap = new HashMap<String,ConnectAgent>();
		m_devicemap = new HashMap<Integer,ConnectAgent>();
	}

	public IAccount(Integer account_id)
	{
		this();

		m_account_id = account_id;
	}

	public Integer getM_account_id()
	{
		return m_account_id;
	}

	public void Push2Map(ConnectAgent ca, MainCallback callback)
	{
		String app_type = ca.getM_app_type();

		if ( app_type.substring(0,3).equals("app") )//app_pc app_web app_mobile
		{
			ConnectAgent atca = null;

			synchronized (m_map_mutex)
			{
				atca = m_apptypemap.get(app_type);

				if ( atca == ca )
					return;

/*
				if (atca != null && atca.getM_remotehost() != null &&
						atca.getM_remotehost().equals(ca.getM_remotehost()) &&
						atca.getM_remoteport() == ca.getM_remoteport())
					return;
				else if ( atca != null && atca.getM_remotehost() == null &&
					atca.getM_lifename().equals(ca.getM_lifename()))
					return;
*/

				ca.setM_iaccount(this);
				m_apptypemap.put(app_type, ca);

				if (atca == null)
				{
					if (callback != null)
						callback.UpdateAccount(true, ca);
				} else
				{
					if (callback != null)
						if (ca.getM_type() == MainServer.Server_type_Server)
							callback.UpdateAccount(true, ca);
						else
							callback.UpdateAccount(false, ca);

					if (ca.getM_type() == MainServer.Server_type_Server)
						atca.unauthorize(false);
					else
						atca.Die();

					atca.setM_iaccount(null);
				}
			}
		}else //dev_camera dev_monitor
		{
			ConnectAgent atca = null;

			synchronized (m_map_mutex)
			{
				atca = m_devicemap.get(ca.getM_device_id());

				if ( atca == ca )
					return;

/*
				if (atca != null && atca.getM_remotehost() != null &&
						atca.getM_remotehost().equals(ca.getM_remotehost()) &&
						atca.getM_remoteport() == ca.getM_remoteport())
					return;
				else if ( atca != null && atca.getM_remotehost() == null &&
						atca.getM_lifename().equals(ca.getM_lifename()))
					return;
*/

				ca.setM_iaccount(this);
				m_devicemap.put(ca.getM_device_id(), ca);

				if (atca == null)
				{
					if (callback != null)
						callback.UpdateDevice(true, ca);
				} else
				{
					if (callback != null)
						if (ca.getM_type() == MainServer.Server_type_Server)
							callback.UpdateDevice(true, ca);
						else
							callback.UpdateDevice(false, ca);

					if (ca.getM_type() == MainServer.Server_type_Server)
						atca.unauthorize(false);
					else
						atca.Die();

					atca.setM_iaccount(null);
				}
			}
		}
	}

	public void Offline2Map(ConnectAgent ca, MainCallback callback)
	{
		String app_type = ca.getM_app_type();

		if ( app_type.substring(0,3).equals("app") )//app_pc app_web app_mobile
		{
			int size = 0;
			ConnectAgent atca = null;

			synchronized (m_map_mutex)
			{
				atca = m_apptypemap.get(app_type);
				if ( atca == ca )
				{
					m_apptypemap.remove(app_type);
					ca.setM_iaccount(null);
					size = m_apptypemap.size();

					if (callback != null)
						callback.OfflineAccount(size, ca);
				}
			}
		}else //dev_camera dev_monitor
		{
			ConnectAgent atca = null;
			synchronized (m_map_mutex)
			{
				atca = m_devicemap.get(ca.getM_device_id());
				if ( atca == ca )
				{
					m_devicemap.remove(ca.getM_device_id());
					ca.setM_iaccount(null);
					if (callback != null)
						callback.OfflineDevice(ca);
				}
			}
		}
	}

	public ConnectAgent GetCA(String app_type)
	{
		ConnectAgent ca = null;

		synchronized (m_map_mutex)
		{
			ca = m_apptypemap.get(app_type);
		}

		return ca;
	}

	public ConnectAgent[] GetCAS()
	{
		synchronized (m_map_mutex)
		{
			ConnectAgent[] rtn = new ConnectAgent[m_apptypemap.size()];

			return m_apptypemap.values().toArray(rtn);
		}
	}
}
